package com.jair.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jair.common.bean.ItemCatResult;
import com.jair.manage.pojo.ItemCat;
import com.jair.manage.service.ItemCatService;

@RequestMapping("item/cat")
@Controller
public class ItemCatController {

	@Autowired
	private ItemCatService itemCatService;

	/**
	 * 查询商品类目数据
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<ItemCat>> queryItemCatList(
			@RequestParam(value = "id", defaultValue = "0") Long parentId) {
		try {
			List<ItemCat> itemCats = this.itemCatService.queryItemCatList(parentId);
			if (itemCats == null || itemCats.isEmpty()) {
				// 资源不存在，返回404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(itemCats);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 资源不存在，返回500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 * 按照前台系统结构返回商品类目数据
	 * 
	 * @return
	 */
	@RequestMapping(value = "all", method = RequestMethod.GET)
	public ResponseEntity<ItemCatResult> queryItemCatAll() {
		return ResponseEntity.ok(this.itemCatService.queryAllToTree());
	}
}
